﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;

namespace MailScheduler
{
    public partial class ThisAddIn
    {
		#region Fields

		private MailItem m_currentMailItem;

		#endregion Fields

		#region Application Event Handlers

		private void ThisAddIn_Startup(object sender, System.EventArgs e)
		{
			Application.ItemLoad += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemLoadEventHandler(Application_ItemLoad);
		}

		void Application_ItemLoad(object Item)
		{
			if (!(Item is MailItem))
			{
				return;
			}

			try
			{
				MailItem mailItem = Item as MailItem;

				HandleMailItemLoad(mailItem);
			}
			catch (System.Exception ex)
			{
				ShowError(ex.ToString());
			}
		}

		private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
		{
			m_currentMailItem = null;
		}

		#endregion Application Event Handlers

		#region Methods

		private void HandleMailItemLoad(MailItem item)
		{
			m_currentMailItem = item;
		}

		/// <summary>
		/// Schedules the send to the next working day at 8 and sends the mail.
		/// </summary>
		/// <remarks>The working week is currently calculated between Sunday and Thursday.</remarks>
		public void ScheduleSend()
		{
			try
			{
				// Find the date and time the item should be delivered.			
				DateTime sendMailOn = CalculateSendingTime();

				// Show the user the time and ask them to approve.
				DialogResult result = System.Windows.Forms.MessageBox.Show("This mail is going to be sent on: " + sendMailOn, "Scheduled Send", System.Windows.Forms.MessageBoxButtons.OKCancel);

				if (result == DialogResult.Cancel)
				{
					return;
				}

				// Schedule the send
				m_currentMailItem.DeferredDeliveryTime = sendMailOn;

				// Send!
				m_currentMailItem.Send();
			}
			catch (System.Exception e)
			{
				ShowError(e.ToString());
			}
		}

		/// <summary>
		/// Calculates the sending time for the mail.
		/// </summary>
		/// <returns>The date time object representing the time to send the mail.</returns>
		private static DateTime CalculateSendingTime()
		{
			int daysToAdd = 1;

			if (DateTime.Today.DayOfWeek == DayOfWeek.Thursday) daysToAdd = 3;
			else if (DateTime.Today.DayOfWeek == DayOfWeek.Friday) daysToAdd = 2;

			DateTime sendMailOn = DateTime.Now.AddDays(daysToAdd);
			sendMailOn = new DateTime(sendMailOn.Year, sendMailOn.Month, sendMailOn.Day, 8, 0, 0);
			return sendMailOn;
		}

		private void ShowError(string message)
		{
			System.Windows.Forms.MessageBox.Show(message,
				  "Error",
				  System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
		}

		#endregion Methods        

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
